package cn.tedu.appointment.accounts.service.impl;

import cn.tedu.appointment.accounts.mapper.StaffMapper;
import cn.tedu.appointment.accounts.pojo.dto.StaffLoginParam;
import cn.tedu.appointment.accounts.pojo.dto.StaffSearchParam;
import cn.tedu.appointment.accounts.pojo.entity.Staff;
import cn.tedu.appointment.accounts.pojo.vo.StaffVO;
import cn.tedu.appointment.accounts.service.StaffService;
import cn.tedu.appointment.base.exception.ServiceException;
import cn.tedu.appointment.base.response.JsonResult;
import cn.tedu.appointment.base.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Random;

@Slf4j
@Service
public class StaffServiceImpl implements StaffService {

    @Autowired
    StaffMapper staffMapper;

    @Override
    public StaffVO login(StaffLoginParam staffLoginParam) {
        log.debug("用户登录业务:staffLoginParam={}", staffLoginParam);
        StaffVO staffVO = staffMapper.selectByName(staffLoginParam);
        if (staffLoginParam.getPhone().isEmpty()) {
            if (staffVO == null) {
                throw new ServiceException(StatusCode.INVALID_USERNAME_OR_PASSWORD);
            }
            if (!staffLoginParam.getPassword().equals(staffVO.getPassword())) {
                throw new ServiceException(StatusCode.INVALID_USERNAME_OR_PASSWORD);
            }
        }else {
            if (staffVO == null) {
                throw new ServiceException(StatusCode.INVALID_USERNAME_OR_PASSWORD);
            }
        }
        return staffVO;
    }

    @Override
    public String getVerify() {
        String verify = "";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            int type = random.nextInt(3);
            switch (type) {
                case 0:
                    char verify1 = (char) (random.nextInt(26) + 65);
                    verify += verify1;
                    break;
                case 1:
                    char verify2 = (char) (random.nextInt(26) + 97);
                    verify += verify2;
                    break;
                case 2:
                    int verify3 = random.nextInt(10);
                    verify += verify3;
                    break;
            }
        }
        return verify;
    }

    @Override
    public List<StaffVO> searchStaff(StaffSearchParam staffSearchParam) {
        log.debug("搜索员工业务:staffLoginParam={}",staffSearchParam);
        List<StaffVO> staffVOList = staffMapper.select(staffSearchParam);
        return staffVOList;
    }
}
